﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LX.Core;
using LX.Web.Excel;
namespace S4Store.Domain.Report
{
    public class CarSaleReport : CarSaleCommonReport
    {
        [Export("合计标题",Colspan=1)]
        public  string HjTitle
        {
            get
            {
                return "合计";
            }
        }

        public List<CarBrandSaleReport> CarBrandSaleReports { get; set; }
        public CarSaleReport()
        {
            CarBrandSaleReports = new List<CarBrandSaleReport>();
        }

        public override void ShowLastDay(bool value)
        {
            CarBrandSaleReports.ForEach(t => t.ShowLastDay(value));
        }

        /// <summary>
        /// 本店交车
        /// </summary>
        [Export("本店交车")]
        public override int BDYJ
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.BDYJ);
            }
        }

        /// <summary>
        /// 二网交车
        /// </summary>
        [Export("二网交车")]
        public override int EWYJ
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.EWYJ);
            }
        }


        /// <summary>
        /// 本店已订
        /// </summary>
        [Export("本店已订")]
        public override int BDYD
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.BDYD);
            }
        }

        /// <summary>
        /// 二网已订
        /// </summary>
        [Export("二网已订")]
        public override int EWYD
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.EWYD);
            }
        }

        /// <summary>
        /// 订单目标
        /// </summary>
        /// </summary>
        [Export("订单目标")]
        public override int OrderTarget
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.OrderTarget);
            }
        }
        ///// <summary>
        ///// 销量进度差
        ///// </summary>
        //public override decimal SaleRateDiff
        //{
        //    get
        //    {
        //        return CarBrandSaleReports.AverageOrDefault(t => t.SaleRateDiff);
        //    }
        //}

        /// <summary>
        /// 销售目标
        /// </summary>
        [Export("销售目标")]
        public override int SaleTarget
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.SaleTarget);
            }
        }

        /// <summary>
        /// A类库存
        /// </summary>
        [Export("A类库存")]
        public override int AKC
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.AKC);
            }
        }

        /// <summary>
        /// B类库存
        /// </summary>
        [Export("B类库存")]
        public override int BKC
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.BKC);
            }
        }

        /// <summary>
        /// C类库存
        /// </summary>
        [Export("C类库存")]
        public override int CKC
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.CKC);
            }
        }
        /// <summary>
        /// 在途
        /// </summary>
        [Export("在途")]
        public override int ZT
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.ZT);
            }
        }
        /// <summary>
        /// C类库龄
        /// </summary>
        [Export("C类库龄")]
        public override int CKL
        {
            get
            {
                return CarBrandSaleReports.Where(t => t.CKC > 0).AverageOrDefault(t => t.CKL).AutoCeling();
            }
        }
        /// <summary>
        /// 预提
        /// </summary>
        [Export("预提")]
        public override int YT
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.YT);
            }
        }

        /// <summary>
        /// 上月库存
        /// </summary>
        [Export("上月库存")]
        public override int LastMonthInventory
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.LastMonthInventory);
            }
        }


        /// <summary>
        /// 上月交车
        /// </summary>
        [Export("上月交车")]
        public override int LastMonthGiveCar
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.LastMonthGiveCar);
            }
        }

        /// <summary>
        /// 上月库存动态系数
        /// </summary>
        [Export("上月库存系数")]
        public override decimal LastMonthInventoryCoefficient
        {
            get
            {
                return CarBrandSaleReports.AverageOrDefault(t => t.LastMonthInventoryCoefficient);
            }
        }

        ///// <summary>
        ///// 销量进度
        ///// </summary>
        //public override decimal SaleRate
        //{
        //    get
        //    {
        //        return CarBrandSaleReports.Where(t => t.SaleTarget > 0).AverageOrDefault(t => t.SaleRate);
        //    }
        //}

        ///// <summary>
        ///// 预计登记客户数
        ///// </summary>
        //public override int EstimatedRegisterCustomerCount
        //{
        //    get
        //    {
        //        return CarBrandSaleReports.Sum(t => t.EstimatedRegisterCustomerCount);
        //    }
        //}

        /// <summary>
        /// 登记客户进度差
        /// 登记客户进度差率=当月登记客户完成进度-时间进度
        /// 当月登记客户完成进度=首次来店客户登记数/首次来店登记数目标
        /// 首次来店登记数目标=当月销量目标/上月平均成交率
        /// </summary>
        public override decimal RegisterCustomerRateDiff
        {
            get
            {
                return CarBrandSaleReports.Where(t => t.EstimatedRegisterCustomerCount > 0).AverageOrDefault(t => t.RegisterCustomerRateDiff);
            }
        }

        ///// <summary>
        ///// 库存动态系数
        ///// </summary>
        //public override decimal InventoryCoefficient
        //{
        //    get
        //    {
        //        return CarBrandSaleReports.Sum(t => t.InventoryCoefficient);
        //    }
        //}


        /// <summary>
        /// 投保数量
        /// </summary>
        [Export("累计投保")]
        public override decimal TBSL
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.TBSL);
            }
        }

        /// <summary>
        /// 投保比例
        /// </summary>
        [Export("投保比率", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public override decimal TBBL
        {
            get
            {
                if (GiveCarCount == 0) return 0;
                return (decimal)TBSL / (decimal)GiveCarCount * 100;
                //return CarBrandSaleReports.Where(t => t.GiveCarCount > 0).AverageOrDefault(t => t.TBBL);
            }
        }

        /// <summary>
        /// 投保比例差
        /// </summary>
        [Export("投保比率差距", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public override decimal TBDiff
        {
            get
            {
                return TBBL - CarKpiMonthConfig.InsureTarget;
                //return CarBrandSaleReports.Where(t => t.GiveCarCount > 0).AverageOrDefault(t => t.TBDiff);
            }
        }

        /// <summary>
        /// 置换数量
        /// </summary>
        [Export("累计置换")]
        public override decimal ZHSL
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.ZHSL);
            }
        }

        /// <summary>
        /// 置换比例
        /// </summary>
        [Export("置换比率", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public override decimal ZHBL
        {
            get
            {
                if (GiveCarCount == 0) return 0;
                return (decimal)ZHSL / (decimal)GiveCarCount * 100;
                //return CarBrandSaleReports.Where(t => t.GiveCarCount > 0).AverageOrDefault(t => t.ZHBL);
            }
        }

        /// <summary>
        /// 置换比例差
        /// </summary>
        [Export("置换比率差距", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public override decimal ZHDiff
        {
            get
            {
                return ZHBL - CarKpiMonthConfig.ReplacementTarget;
                //return CarBrandSaleReports.Where(t => t.GiveCarCount > 0).AverageOrDefault(t => t.ZHDiff);
            }
        }

        /// <summary>
        /// 按揭数量
        /// </summary>
        [Export("累计按揭")]
        public override decimal AJSL
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.AJSL);
            }
        }

        /// <summary>
        /// 按揭比例
        /// </summary>
        [Export("按揭比率", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public override decimal AJBL
        {
            get
            {
                if (GiveCarCount == 0) return 0;
                return (decimal)AJSL / (decimal)GiveCarCount * 100;
                //return CarBrandSaleReports.Where(t => t.GiveCarCount > 0).AverageOrDefault(t => t.AJBL);
            }
        }

        /// <summary>
        /// 按揭比例差
        /// </summary>
        [Export("按揭比率差距", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public override decimal AJDiff
        {
            get
            {
                return AJBL - CarKpiMonthConfig.FinancialTarget;
                //return CarBrandSaleReports.Where(t => t.GiveCarCount > 0).AverageOrDefault(t => t.AJDiff);
            }
        }

        /// <summary>
        /// 延保数量
        /// </summary>
        [Export("累计延保")]
        public override decimal YBSL
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.YBSL);
            }
        }

        /// <summary>
        /// 延保比例
        /// </summary>
        [Export("延保比率", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public override decimal YBBL
        {
            get
            {
                if (GiveCarCount == 0) return 0;
                return (decimal)YBSL / (decimal)GiveCarCount * 100;
                //return CarBrandSaleReports.Where(t => t.GiveCarCount > 0).AverageOrDefault(t => t.YBBL);
            }
        }

        /// <summary>
        /// 延保比例差
        /// </summary>
        [Export("延保比率差距", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public override decimal YBDiff
        {
            get
            {
                return YBBL - CarKpiMonthConfig.ExtendTarget;
                //return CarBrandSaleReports.Where(t => t.GiveCarCount > 0).AverageOrDefault(t => t.YBDiff);
            }
        }





        [Export("上旬版本计数")]
        public int LstVersionHolder
        {
            get { return CarBrandSaleReports.Sum(t => t.LstVersionHolder); }
        }

        [Export("中旬版本计数")]
        public int MidVersionHolder
        {
            get { return CarBrandSaleReports.Sum(t => t.MidVersionHolder); }
        }

        [Export("下旬版本计数")]
        public int LastVersionHolder
        {
            get { return CarBrandSaleReports.Sum(t => t.LastVersionHolder); }
        }

        /// <summary>
        /// 上旬套餐版本交车量
        /// </summary>
        [Export("上旬套餐版本交车量")]
        public override int LstTenVersionCount
        {
            get { return CarBrandSaleReports.Sum(t => t.LstTenVersionCount); }
        }

        /// <summary>
        /// 中旬套餐版本交车量
        /// </summary>
        [Export("中旬套餐版本交车量")]
        public override int MidTenVersionCount
        {
            get { return CarBrandSaleReports.Sum(t => t.MidTenVersionCount); }
        }

        /// <summary>
        /// 下旬套餐版本交车量
        /// </summary>
        [Export("下旬套餐版本交车量")]
        public override int LastTenVersionCount
        {
            get { return CarBrandSaleReports.Sum(t => t.LastTenVersionCount); }
        }

        [Export("累计版本计数")]
        public int VersionCount
        {
            get { return LstVersionHolder + MidVersionHolder + LastVersionHolder; }
        }


        public override int Bpcxjs
        {
            get { return CarBrandSaleReports.Sum(t => t.Bpcxjs); }
        }

        /// <summary>
        /// 装潢车交车量
        /// </summary>
        [Export("装潢车交车量")]
        public override int DecorateCount
        {
            get { return CarBrandSaleReports.Sum(t => t.DecorateCount); }
        }

        /// <summary>
        /// 装潢总额
        /// </summary>
        [Export("装潢总额")]
        public override decimal DecorateAmout
        {
            get { return CarBrandSaleReports.Sum(t => t.DecorateAmout); }
        }

        /// <summary>
        /// 单车装潢总额
        /// </summary>
        [Export("单车装潢总额")]
        public override decimal DecorateAmoutOfSingCar
        {
            get
            {
                return CarBrandSaleReports.Where(t => t.DecorateAmoutOfSingCar > 0).AverageOrDefault(t => t.DecorateAmoutOfSingCar);
            }
        }

        /// <summary>
        /// 套餐车销售率
        /// </summary>
        [Export("套餐车销售率", Format = "{0}%")]
        public override decimal PackageXSLV
        {
            get
            {
                if (GiveCarCount == 0) return 0;
                return (decimal)PackageCount / (decimal)GiveCarCount * 100;
                //return CarBrandSaleReports.Where(t => t.GiveCarCount > 0).AverageOrDefault(t => t.PackageXSLV);
            }
        }

        /// <summary>
        /// 套餐车销售量差距
        /// </summary>
        [Export("套餐车销售量差距")]
        public override int PackageCountDiff
        {
            get
            {
                return ((decimal)PackageCount -  CarKpiMonthConfig.PakeageSaleRateTagrt * (decimal)GiveCarCount/(decimal)100).AutoRound1();
            }
        }

        /// <summary>
        /// 套餐车销售量
        /// </summary>
        [Export("套餐车销售量")]
        public override int PackageCount
        {
            get
            {
                return CarBrandSaleReports.Sum(o => o.PackageCount);
            }
        }


        /// <summary>
        /// 套餐车销售率差
        /// </summary>
        [Export("套餐车销售率差", Format = "{0}%")]
        public override decimal PackageDiff
        {
            get
            {
                return PackageXSLV - CarKpiMonthConfig.PakeageSaleRateTagrt;
                //return CarBrandSaleReports.AverageOrDefault(t => t.PackageDiff);
            }
        }

        /// <summary>
        /// 装潢车销售率
        /// </summary>
        [Export("装潢车销售率", Format = "{0}%")]
        public override decimal DecorateXSLV
        {
            get
            {
                if (GiveCarCount == 0) return 0;
                return (decimal)DecorateCount / (decimal)GiveCarCount * 100;
                //return CarBrandSaleReports.AverageOrDefault(t => t.DecorateXSLV);
            }
        }


        /// <summary>
        /// 装潢车销售量差距
        /// </summary>
        [Export("装潢车销售量差距")]
        public override int DecorateCountDiff
        {
            get
            {
                return ((decimal)DecorateCount - CarKpiMonthConfig.DecorateSaleRateTagrt * (decimal)GiveCarCount / (decimal)100).AutoRound1();
                //return CarBrandSaleReports.Sum(t => t.DecorateCountDiff);
            }
        }


        /// <summary>
        /// 套餐车型数
        /// </summary>
        public override int PackageCarModelCount
        {
            get
            {
                return CarBrandSaleReports.Sum(t => t.PackageCarModelCount);
            }
        }


        /// <summary>
        /// 套餐车设计率
        /// </summary>
        public override decimal PackageCarSJLV
        {
            get
            {
                if (CarBrandSaleReports.Sum(t=>t.CarModelSaleReports.Count) == 0) return 0;
                return (decimal)PackageCarModelCount * 100 / (decimal)(CarBrandSaleReports.Sum(t => t.CarModelSaleReports.Count)*3);
            }
        }



        /// <summary>
        /// 套餐车更换率
        /// </summary>
        [Export("套餐车更换率", Format = "{0}%")]
        public override decimal PackageVersionChangeLv
        {
            get
            {
                return CarBrandSaleReports.AverageOrDefault(t => t.PackageVersionChangeLv);
            }
        }

        /// <summary>
        /// H有望客户
        /// </summary>
        [Export("H级有望客户")]
        public override int HYWKH
        {
            get { return CarBrandSaleReports.Sum(t => t.HYWKH); }
        }

        /// <summary>
        /// A有望客户
        /// </summary>
        [Export("A级有望客户")]
        public override int AYWKH
        {
            get { return CarBrandSaleReports.Sum(t => t.AYWKH); }
        }
        /// <summary>
        /// B有望客户
        /// </summary>
        [Export("B级有望客户")]
        public override int BYWKH
        {
            get { return CarBrandSaleReports.Sum(t => t.BYWKH); }
        }

        /// <summary>
        /// H登记客户
        /// </summary>
        [Export("H级登记客户")]
        public override int HDJKH
        {
            get { return CarBrandSaleReports.Sum(t => t.HDJKH); }
        }

        /// <summary>
        /// A登记客户
        /// </summary>
        [Export("A级登记客户")]
        public override int ADJKH
        {
            get { return CarBrandSaleReports.Sum(t => t.ADJKH); }
        }
        /// <summary>
        /// B登记客户
        /// </summary>
        [Export("B级登记客户")]
        public override int BDJKH
        {
            get { return CarBrandSaleReports.Sum(t => t.BDJKH); }
        }
        /// <summary>
        /// 销量数量差
        /// </summary>
        [Export("登记客户合计", BackBround = ExcelCellBg.Calculate)]
        public override decimal SaleAmountDiff
        {
            get { return CarBrandSaleReports.Sum(t => t.SaleAmountDiff); }
        }
        /// <summary>
        /// 预期销量
        /// </summary>
        public override decimal SaleExpectTarget
        {
            get { return CarBrandSaleReports.Sum(t => t.SaleExpectTarget); }
        }

        /// <summary>
        /// 库存目标
        /// </summary>
        public override decimal InventoryTarget
        {
            get { return CarBrandSaleReports.Sum(t => t.InventoryTarget); }
        }
    }
}
